// views/mine/mycollect/index.js
var util = require('../../../utils/util')
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    focuses: '',
    fans: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.request({
      url: `${BASE_URL}/user/guanzhu.html`,
      method: "POST",
      data: {
        id: userinfo.id
      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
        // 'Content-Type': 'application/json'
      },
      success(res) {
        res.data.list.forEach(item => {
          if (item.avatar.length < '60' && item.avatar.length !== 0) {
            item.avatar = `${BASE_URL}/` + item.avatar
          }
          if (item.avatar.length == 0) {
            item.avatar = `${BASE_URL}//uploads/touxiang/m_5d54f81ee9739.gif`
          }
          item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
        })
        res.data.bei.forEach(item => {
          if (item.avatar.length < '60' && item.avatar.length !== 0) {
            item.avatar = `${BASE_URL}/` + item.avatar
          }
          if (item.avatar.length == 0) {
            item.avatar = `${BASE_URL}//uploads/touxiang/m_5d54f81ee9739.gif`
          }
          item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
        })
        console.log(res)
        _this.setData({
          focuses: res.data.list,
          fans: res.data.bei
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onChange(event) {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gohomepage: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/views/mine/otherhomepage/index?id=${id}&flag=${true}`
    })
  },
  closefocus: function (e) {
    var _this = this
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定要取消关注吗',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var userinfo = wx.getStorageSync("USER")
          wx.request({
            // url: `${BASE_URL}/user/qvxiaoguanzhu.html`,
            url: `${BASE_URL}/user/qvxiaoguanzhu/id/${e.currentTarget.dataset.id }/uid/${userinfo.id}`,
            method: "get",
            header: {
              'Content-type': 'application/x-www-form-urlencoded'
              // 'Content-Type': 'application/json'
            },
            success(res) {
              _this.data.focuses.splice(e.currentTarget.dataset.index, 1)
              _this.setData({
                focuses: _this.data.focuses
              })
              var info = wx.getStorageSync('data')
              util.login(info.avatarUrl, info.city, info.country, info.gender, info.language, info.nickName, info.openId, info.province, info.unionId)
              console.log("你点了关注", info)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})